﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain.Extensions
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Web.Mvc;
	using System.Web.Routing;
	using Falcon.Core.Collections;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Extensions;

	public static class UserMessageExtension
	{
		// private fields
		private static readonly Dictionary<string, string> Localized;

		// private constants
		public const string Read = "message_read";
		public const string NotRead = "message_notread";
		public const string Replied = "message_replied";
		public const string Forwarded = "message_forwarded";

		/// <summary>
		/// Initializes the <see cref="UserMessageExtension"/> class.
		/// </summary>
		static UserMessageExtension()
		{
			Localized = new Dictionary<string, string>
			{
				{ Read, Resources.Titles.Read },
				{ NotRead, Resources.Titles.NotRead },
				{ Replied, Resources.Titles.Replied },
				{ Forwarded, Resources.Titles.Forwarded }
			};
		}

		/// <summary>
		/// Groups the user messages by today, yesterday, this week and older.
		/// </summary>
		/// <param name="messages">The messages.</param>
		/// <returns></returns>
		public static IPagedList<UserMessageGrouped, UserMessageSpecification> ToGrouped(this IPagedList<UserMessage, UserMessageSpecification> messages)
		{
			var grouped = new List<UserMessageGrouped>
            {
              new UserMessageGrouped(UserMessageGroupedFilter.Today, messages.Items),
              new UserMessageGrouped(UserMessageGroupedFilter.Yesterday, messages.Items),
              new UserMessageGrouped(UserMessageGroupedFilter.ThisWeek, messages.Items),
              new UserMessageGrouped(UserMessageGroupedFilter.Older, messages.Items)
            };

			return new PagedList<UserMessageGrouped, UserMessageSpecification>(
				messages.Specification, 
				grouped, 
				messages.Page, 
				messages.Limit, 
				messages.Total);
		}

		/// <summary>
		/// Gets the status image for the specified user message.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <returns></returns>
		public static string StatusImage(this UserMessage message)
		{
			return StatusImage(message, null);
		}

		/// <summary>
		/// Gets the status image for the specified user message.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="attributes">The attributes.</param>
		/// <returns></returns>
		public static string StatusImage(this UserMessage message, object attributes)
		{
			string key;

			if (message.ReceiverForwarded)
			{
				key = Forwarded;
			}
			else if (message.ReceiverReplied)
			{
				key = Replied;
			}
			else
			{
				key = message.ReceiverRead ? Read : NotRead;
			}

			var text = Localized[key];
			var builder = new TagBuilder("img");

			builder.MergeAttribute("src", string.Concat(Settings.Users.MessageIconPath.AsRoot(), "/", key, ".gif"));
			builder.MergeAttribute("alt", text);
			builder.MergeAttribute("title", text);
			builder.MergeAttributes(new RouteValueDictionary(attributes));

			return builder.ToString(TagRenderMode.SelfClosing);
		}

		/// <summary>
		/// Gets the user message folders.
		/// </summary>
		/// <param name="messages">The messages.</param>
		/// <returns></returns>
		public static SelectList Folders(this IList<UserMessage> messages)
		{
			// get user identity
			var identity = User.Identity;

			// get user message service
			var service = Application.Resolve<IUserMessageFolderService>();

			// get all folders for user
			var folders = service.GetAll(new UserMessageFolderSpecification
			{
				UserId = identity.Id
			});

			// add inbox folder
			folders.Add(service.Inbox());

			return new SelectList(folders, "Id", "Title", 0);
		}

		/// <summary>
		/// Gets the user message actions.
		/// </summary>
		/// <param name="contacts">The contacts.</param>
		/// <returns></returns>
		public static SelectList Actions(this IPagedList<UserMessageGrouped, UserMessageSpecification> contacts)
		{
			return new SelectList(Enum.GetNames(typeof(UserMessageAction)), UserMessageAction.MoveToFolder.Name());
		}

		/// <summary>
		/// Gets the restricted user message actions.
		/// </summary>
		/// <param name="contacts">The contacts.</param>
		/// <returns></returns>
		public static SelectList ActionsRestricted(this IPagedList<UserMessageGrouped, UserMessageSpecification> contacts)
		{
			// get the names
			var names = Enum.GetNames(typeof(UserMessageAction));

			// copy only allowed names to list
			var values = names.Where(x => 
				!x.Equals("MoveToFolder") && 
				!x.Equals("MarkRead") && 
				!x.Equals("MarkNotRead")).ToList();

			return new SelectList(values, UserMessageAction.Delete.Name());
		}
	}
}